package com.hss.realmtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmList;
import io.realm.RealmResults;
/*
* https://blog.csdn.net/u012426327/article/details/77992865
*Realm数据库使用总结及采坑记录
* https://www.jianshu.com/p/b4b05c30201f
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final  String TAG = MainActivity.class.getSimpleName();
    Realm mRealm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        mRealm = Realm.getDefaultInstance();// // opens "myrealm.realm"


        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("myrealm.realm") //文件名
                .inMemory()
                .schemaVersion(0) //版本号
                .build();
        mRealm = Realm.getInstance(config);

        findViewById(R.id.button).setOnClickListener(this);

        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);




     }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRealm != null){
            mRealm.close();
        }
    }

    /*@Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                mRealm.beginTransaction();
                Book book1 = mRealm.createObject(Book.class);
                book1.name = "book1";
                book1.price  = 20.5f;
                mRealm.commitTransaction();
                break;
            case R.id.button2:
                Book book2 = new Book();
                book2.name = "book2";
                book2.price = 58.0f;
                mRealm.beginTransaction();
                mRealm.copyToRealm(book2);
                mRealm.commitTransaction();
                break;
            case R.id.button3:
                final Book book3 = new Book();
                book3.name = "book3";
                book3.price = 18.2f;
                mRealm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.copyToRealm(book3);
                    }
                });
                break;
            case R.id.button4:
                final RealmResults<Book> userList = mRealm.where(Book.class).findAll();
                for (Book book: userList){
                    Log.e(TAG, book.toString());
                }
                break;
        }
    }*/

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Book book1 =  new Book();
                book1.name = "book1";
                book1.price  = 20.5f;
                Book book2 = new Book();
                book2.name = "book2";
                book2.price = 58.0f;

                Student student = new Student();
                student.name = "stu1";
                student.books = new RealmList<>();
                student.books.add(book1);
                student.books.add(book2);
                mRealm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.copyToRealm(student);
                    }
                });
                break;
            case R.id.button2:
                mRealm.beginTransaction();
                Book book4 = mRealm.createObject(Book.class,5);
                book4.name = "book1";
                book4.price  = 20.5f;
                mRealm.commitTransaction();
                break;
            case R.id.button3:
                final Book book3 = new Book();
                book3.name = "book3";
                book3.price = 18.2f;
                mRealm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.copyToRealm(book3);
                    }
                });
                break;
            case R.id.button4:
                final RealmResults<Student> userList = mRealm.where(Student.class).findAll();
                for (Student book: userList){
                    Log.e(TAG, book.toString());
                }
                break;
        }
    }
}
